//
//  RecentReadTopView.m
//  ebook
//
//  Created by 吴超飞 on 16/7/18.
//  Copyright © 2016年 Moton. All rights reserved.
//

#import "AppConfigManager.h"
#import "BookcaseCellView.h"
#import "RecentReadTopView.h"
#import "UIView+Extension.h"

@implementation RecentReadTopView

-(instancetype)initWithFrame:(CGRect)frame{
   
    self = [super initWithFrame:frame];
    
    if(self){
        [self reDraw];
    }
    
    return self;
}

-(void)setHasNoRecentRead:(BOOL)hasNoRecentRead{
    
    _hasNoRecentRead = hasNoRecentRead;
    
    if (hasNoRecentRead) {
        [self addSubview:self.noRecentImgview];
        [self addSubview:self.noRecentLabel];
        
        [self hideView:YES];
    }else{
        [self.noRecentImgview removeFromSuperview];
        [self.noRecentLabel removeFromSuperview];
        
        [self hideView:NO];
        [self setBook:self.book];
    }
}

-(UILabel *)noRecentLabel{
    if (!_noRecentLabel) {
        _noRecentLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.noRecentImgview.frame)-6, self.size.width, 30)];
        _noRecentLabel.text = @"最近阅读的书会在这里";
        _noRecentLabel.textColor = [UIColor whiteColor];
        _noRecentLabel.alpha = 0.8f;
        _noRecentLabel.font = [UIFont systemFontOfSize:12];
        _noRecentLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _noRecentLabel;
}

-(UIImageView *)noRecentImgview{
    if(!_noRecentImgview){
        float width = 70;
        _noRecentImgview = [[UIImageView alloc] initWithFrame:CGRectMake((self.frame.size.width-width)/2, 16, width, width)];
        _noRecentImgview.image = [UIImage imageNamed:@"shuchongicon"];
        _noRecentImgview.alpha = 0.8f;
    }
    return _noRecentImgview;
}

-(void)hideView:(BOOL)hide{
 
    self.bgView.hidden = hide;
    self.coverImageview.hidden = hide;
    self.booknameLabel.hidden = hide;
    self.readedPercentLabel.hidden = hide;
    self.bookStatusLabel.hidden = hide;
    self.recentChapterLabel.hidden = hide;
    self.continueReadBtn.hidden = hide;
    
    self.foreBgview.hidden = YES;
    self.deleteBtn.hidden = YES;
}

-(void)reDraw{//在普通单元格的基础上调整一下元素的位置及文字大小属性等
    
    self.layer.opaque = NO;
    self.opaque = NO;
    self.backgroundColor = [UIColor clearColor];
    
    CGRect bgViewFrame = self.bgView.frame;
    bgViewFrame.origin.y += 7;
    bgViewFrame.size.width *= kRate;
    bgViewFrame.size.height *= kRate;
    bgViewFrame.origin.x = kMargin;
    self.bgView.frame = bgViewFrame;
    self.bgView.image = [UIImage new];//隐藏白色背景
    
    self.progressView.width *= kRate;
    self.progressView.height *= kRate;
    
    self.coverImageview.width *= kRate;
    self.coverImageview.height *= kRate;
    self.coverImageview.layer.shadowColor = [UIColor blackColor].CGColor;
    self.coverImageview.layer.shadowOffset = CGSizeMake(0,0);
    self.coverImageview.layer.shadowOpacity = 0.4;
    self.coverImageview.layer.shadowRadius = 3.0;//给imageview添加阴影和边框
    
    self.foreBgview.width *= kRate;
    self.foreBgview.height *= kRate;
    self.reImageView.width *= kRate;
    self.reImageView.height *= kRate;
    self.reImageView.x = self.coverImageview.width-self.reImageView.width-2;
    
    self.deleteBtn.center = self.bgView.center;
    self.foreBgview.center = self.bgView.center;
    
    CGRect booknameLabelFrame = self.booknameLabel.frame;
    booknameLabelFrame.origin.x = CGRectGetMaxX(self.bgView.frame)+20;
    booknameLabelFrame.origin.y = CGRectGetMinY(self.bgView.frame)+4;
    booknameLabelFrame.size.width = MTScreenW - kMargin - booknameLabelFrame.origin.x;
    booknameLabelFrame.size.height = 30;
    self.booknameLabel.frame = booknameLabelFrame;
    self.booknameLabel.font = [UIFont systemFontOfSize:18];
    self.booknameLabel.backgroundColor = [UIColor clearColor];
    self.booknameLabel.textColor = [UIColor whiteColor];
    
    //新增－最近阅读章节
    CGRect recentFrame = CGRectMake(self.booknameLabel.frame.origin.x, CGRectGetMaxY(booknameLabelFrame) - 4, booknameLabelFrame.size.width, 20);
    if(!_recentChapterLabel)_recentChapterLabel = [[UILabel alloc] initWithFrame:recentFrame];
    _recentChapterLabel.textColor = [UIColor whiteColor];
    _recentChapterLabel.font = [UIFont systemFontOfSize:12];
    _recentChapterLabel.alpha = 0.6f;
    [self addSubview:_recentChapterLabel];
    
    CGRect bookReadedPercentLabelFrame = self.readedPercentLabel.frame;
    bookReadedPercentLabelFrame.origin.x = booknameLabelFrame.origin.x;
    bookReadedPercentLabelFrame.origin.y = CGRectGetMaxY(self.bgView.frame) - 20;
    bookReadedPercentLabelFrame.size.width = 200;
    self.readedPercentLabel.frame = bookReadedPercentLabelFrame;
    self.readedPercentLabel.textAlignment = NSTextAlignmentLeft;
    self.readedPercentLabel.font = [UIFont systemFontOfSize:14];
    self.readedPercentLabel.backgroundColor = [UIColor clearColor];
    self.readedPercentLabel.textColor = [UIColor whiteColor];
    self.readedPercentLabel.alpha = 0.6f;
    
    CGRect bookStatusLabelFrame = self.bookStatusLabel.frame;
    bookStatusLabelFrame.origin.x = booknameLabelFrame.origin.x;
    bookStatusLabelFrame.origin.y = CGRectGetMinY(self.readedPercentLabel.frame) - 20;
    self.bookStatusLabel.frame = bookStatusLabelFrame;
    self.bookStatusLabel.backgroundColor = [UIColor clearColor];
    self.bookStatusLabel.alpha = 0.6f;
    
    //新增－继续阅读
    float btnWidht = 96;
    _continueReadBtn = [[UIButton alloc] initWithFrame:CGRectMake(self.frame.size.width - btnWidht - kMargin - 3, CGRectGetMaxY(self.bgView.frame) - 25, btnWidht, 26)];
    _continueReadBtn.layer.borderColor = [UIColor whiteColor].CGColor;
    _continueReadBtn.layer.borderWidth = 1;
    _continueReadBtn.layer.cornerRadius = 3;
    _continueReadBtn.titleLabel.font = [UIFont systemFontOfSize:12];
    [_continueReadBtn setTitle:@"继续阅读" forState:UIControlStateNormal];
    [_continueReadBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [_continueReadBtn addTarget:self action:@selector(continueReadBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_continueReadBtn];
}

-(void)continueReadBtnClick:(UIButton *)btn{
    
    if ([self.recentDelegate respondsToSelector:@selector(recentReadTopViewContinueRead)]) {
        [self.recentDelegate recentReadTopViewContinueRead];
    }
}

-(void)update:(Book*)book{
    
    _recentChapterLabel.text = book.rchaptername;
    [self.coverImageview sd_setImageWithURL:[NSURL URLWithString:book.coverpicurl] placeholderImage:[UIImage imageNamed:@"default_book_cover"]];
}

@end
